package com.ecoolex.union.pay.retailer.core.manager;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.union.pay.retailer.core.dao.UnionPayCouponCallRecordDAO;
import com.ecoolex.union.pay.retailer.core.util.unionpay.LogUtil;
import com.ecoolex.union.pay.retailer.entity.UnionPayCouponCallRecordWithBLOBs;
import com.ecoolex.union.pay.retailer.pojo.UnionPayCouponOperateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class UnionPayCouponCallRecordManager {
    @Autowired
    private UnionPayCouponCallRecordDAO dao;

    public UnionPayCouponCallRecordWithBLOBs save(UnionPayCouponOperateRequest request, String orginRequst, String encryptedRequest,
                                                  String encryptedResponse, String decryptedResonse, String createUser, Boolean isSuccess){
        UnionPayCouponCallRecordWithBLOBs record = new UnionPayCouponCallRecordWithBLOBs();
        try {
            record.setOrginRequest(orginRequst);
            record.setEncryptedRequest(encryptedRequest);
            record.setEncryptedResponse(encryptedResponse);
            record.setDecryptedResponse(decryptedResonse);
            record.setOperationType(request.getProcType());
            record.setCouponCode(request.getCouponId());
            if(Check.notNullOrEmpty(request.getRefNumber())){
                record.setOrderNo(request.getRefNumber());
            }
            record.setDeleted(UnionPayCouponCallRecordWithBLOBs.NOT_DELETED);
            record.setCreateUser(createUser);
            record.setCreateTime(Date.from(Instant.now()));
            record.setUpdateUser(createUser);
            record.setUpdateTime(Date.from(Instant.now()));
            record.setIsSuccess(isSuccess);
            dao.insert(record);
        }catch (Exception ignore){
            //忽略异常，避免业务事务回滚
            LogUtil.writeErrorLog("写入调用记录失败",ignore);
        }
        return record;
    }
}
